<template>
  <div>
    <!--     平级规则配置 -->
    <el-row style="text-align: right">
      <el-button type="primary" @click="addUpdataRules" v-show="panePermiss[147]">{{ $t('新增') }}</el-button>
    </el-row>
    <el-row style="margin-top: 10px">
      <Table :page="page" row-key="id" :table-data="tableData"
             @handleSizeChange="handleSizeChange"
             @handleCurrentChange="handleCurrentChange">
        <el-table-column :label="$t('平级规则名称')" prop="rulesName" align="center" min-width="150"/>
          <el-table-column :label="$t('平级方式')" prop="rulesType" align="center"/>
          <el-table-column :label="$t('平级所需逾期天数')" prop="rulesNum" align="center" min-width="150">
            <template slot-scope="scope">
              {{ scope.row.rulesType === $t('按天') ? scope.row.rulesNum : '/' }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('平级所需逾期次数')" prop="rulesNum" align="center" min-width="150">
            <template slot-scope="scope">
              {{ scope.row.rulesType === $t('按次') ? scope.row.rulesNum : '/' }}
            </template>
          </el-table-column>
          <el-table-column :label="$t('创建时间')" prop="createTime" align="center" width="150"></el-table-column>
          <el-table-column :label="$t('更新时间')" prop="updateTime" align="center" width="150"></el-table-column>
          <el-table-column :label="$t('状态')" align="center">
            <template slot-scope="scope">
              <el-switch @change="changeStatus(scope.row)"
                         :disabled="panePermiss[148] ? false : true"
                       :value="scope.row.status"></el-switch>
            </template>
          </el-table-column>
          <el-table-column :label="$t('操作')" align="center" min-width="100">
            <template slot-scope="scope">
              <el-button type="text" @click="editProduct(scope.row)" v-show="panePermiss[149]">{{ $t('编辑') }}</el-button>
              <el-button type="text" @click="deleteProduct(scope.row)" v-show="panePermiss[1058]">{{
                  $t('删除')
                }}
              </el-button>
            </template>
          </el-table-column>
      </Table>
    </el-row>
    <Dialog :visible.sync="visible.isShow" :title="visible.title" @close="clearForm" @cancel="visible.isShow = false"
            @confirm="visibleConfirm" :confirm-loading="confirmLoading">
      <el-form :model="form" ref="form" label-width="150px" :rules="rulesForm">
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item :label="$t('平级规则名称')" prop="rulesName">
              <el-input v-model="form.rulesName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('平级方式')" prop="rulesType">
              <el-select style="width: 100%" v-model="form.rulesType">
                <el-option :label="$t('按天')" :value="1"></el-option>
                <el-option :label="$t('按次')" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="form.rulesType === 1 ? $t('平级所需逾期天数') : $t('平级所需逾期次数')" prop="rulesNum">
              <el-input v-model.number="form.rulesNum"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item :label="$t('描述')" prop="describe">
              <el-input type="textarea" v-model="form.describe"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

    </Dialog>
  </div>
</template>

<script>
import {deepClone} from "@/utils/tools";
import {userLoanRulesList,userLoanRulesDelete,addUserLoanRules,updateUserLoanRules} from "@/api/product";

export default {
  props: ['panePermiss'],
  data() {
    const validatorRulesNum = (rules, value, callBack) => {
      if (value < 1) {
        callBack(new Error(this.$t('不能小于一天')));
      } else if (typeof value !== 'number') {
        callBack(new Error(this.$t('非数字')));
      }
      callBack();
    }
    return {
      tableData: [],
      form: {},
      rulesForm: {
        rulesName: [
          {required: true, message: this.$t('不能为空')},
        ],
        rulesNum: [
          {required: true, message: this.$t('不能为空')},
          {required: true, validator: validatorRulesNum}
        ],
        amount: [
          {required: true, message: this.$t('不能为空')},
        ],
      },
      visible: {
        isShow: false,
        type: '',
        title: '',
      },
      page: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      confirmLoading:false
    }
  },
  mounted() {
    this.getTableData()
  },
  methods: {
    async getTableData() {
      const params = {
        ...this.page,
        type: 2,
        productId: this.$route.query.productId
      }
      const res = await userLoanRulesList(params)
      const {list, total} = res.data
      if (res.code === 1) {
        this.tableData = list.map(item => {
          item.status = item.status === 1
          item.rulesType = item.rulesType === 1 ?  this.$t('按天') : this.$t('按次')
          return item
        })
        this.page = Object.assign({}, this.page, {
          total: total
        })
      }
    },
    clearForm() {
      this.form = {}
      this.$refs.form.resetFields()
    },
    handleSizeChange(val) {
      this.page.pageSize = val
      this.getTableData()
    },
    handleCurrentChange(val) {
      this.page.pageNum = val
      this.getTableData()
    },
    async changeStatus(row) {
      const res = await this.$post('loanProduct/updateUserLoanRulesStatus', {
        id: row.id,
        status: row.status ? 2 : 1,
        productId: this.$route.query.productId,
      })
      if (res.code === 1) {
        this.$message.success(this.$t('修改成功'))
        this.getTableData()
      }
    },
    async deleteProduct({id}) {
      this.$confirm(this.$t('确定删除?'), this.$t('提示'), {
        type: 'warning'
      }).then(async () => {
        const res = await userLoanRulesDelete( {id})
        if (res.code === 1) {
          this.$message({
            type: 'success',
            message: this.$t('删除成功')
          });
          this.getTableData()
        }
      }).catch(() => {
      });
    },
    addUpdataRules() {
      this.visible = {
        isShow: true,
        type: 'add',
        title: this.$t('添加平级规则')
      }
    },
    editProduct(row) {
      const tableRow = deepClone(row)
      this.form = Object.assign({}, tableRow, {
        rulesType: tableRow.rulesType === this.$t('按天') ? 1 : 2
      })
      this.visible = {
        isShow: true,
        type: 'edit',
        title: this.$t('编辑平级规则')
      }
    },
    async visibleConfirm() {
      this.$refs.form.validate(async v => {
        if (v) {
          this.confirmLoading = true
          let message = ''
          let api
          const params = Object.assign({}, this.form, {
            productId: this.$route.query.productId,
            type: 2,
          })
          if (this.visible.type === 'add') {
            api = addUserLoanRules(params)
            message = this.$t('新增成功')
          } else {
            api = updateUserLoanRules(params)
            message = this.$t('修改成功')
          }
          const res = await api
          if (res.code === 1) {
            this.confirmLoading = false
            this.$message.success(message)
            this.clearForm()
            this.visible.isShow = false
            this.getTableData()
          } else {
            this.confirmLoading =false
          }
        }
      })

    },

  }
}
</script>

<style scoped>

</style>
